package com.qiantu.phone.widget.setting;

import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.hjq.http.listener.HttpCallback;
import com.hjq.widget.view.SwitchButton;
import com.qiantu.api.db.LLDeviceDBManager;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.event.DeviceDetailChangeEvent;
import com.qiantu.phone.event.RefreshDeviceListEvent;
import com.qiantu.phone.event.UpdateFavouriteEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DeviceSettingCommon extends LinearLayout {
    private final SwitchButton btnSetCommonSwitch;
    private AppActivity mContext;
    private DeviceBean mDevice;
    ImageView iconDevice;

    public DeviceSettingCommon(AppActivity context, DeviceBean bean) {
        this(context, bean, null);
    }

    public DeviceSettingCommon(AppActivity context, DeviceBean bean, @Nullable AttributeSet attrs) {
        this(context, bean, attrs, 0);
    }

    public DeviceSettingCommon(AppActivity context, DeviceBean bean, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, bean, attrs, defStyleAttr, 0);
    }

    public DeviceSettingCommon(AppActivity context, DeviceBean bean, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        mContext = context;
        mDevice = bean;
        LayoutInflater.from(getContext()).inflate(R.layout.device_setting_common, this, true);
        int dp_16 = getResources().getDimensionPixelSize(R.dimen.dp_16);
        int dp_14 = getResources().getDimensionPixelSize(R.dimen.dp_14);
        setPadding(dp_16, dp_14, dp_16, dp_14);
        setGravity(Gravity.CENTER_VERTICAL);
        btnSetCommonSwitch = findViewById(R.id.btn_set_common_switch);
        btnSetCommonSwitch.setChecked(mDevice.getIsFavourite());
        btnSetCommonSwitch.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setFavourite();
            }
        });
    }

    private void setFavourite() {
        Map<String, Object> data = new HashMap<>();
        //当前选中，就取消常用 没有选中就设为常用
        boolean isFavourite = btnSetCommonSwitch.isChecked();
        data.put("isFavourite", isFavourite);
        List<String> selectedSerialNos = new ArrayList<>();
        selectedSerialNos.add(mDevice.getDeviceSerialNo());
        HttpCallback<HttpData<Void>> callback = new HttpCallback<HttpData<Void>>(mContext) {
            @Override
            public void onSucceed(HttpData<Void> result) {
                if (result.isRequestSucceed()) {
                    boolean needUpdateFavourite = false;
                    if(isFavourite != mDevice.getIsFavourite()){
                        needUpdateFavourite = true;
                    }
                    LLDeviceDBManager.getInstance(getContext()).updateDeviceFavourite(isFavourite, selectedSerialNos);
                    mContext.toast(R.string.set_success);
                    //常用改变首页设备需要刷新
                    EventBus.getDefault().post(new RefreshDeviceListEvent(mDevice.getRoomSerialNo(), needUpdateFavourite));
                    EventBus.getDefault().post(new DeviceDetailChangeEvent(mDevice.getDeviceSerialNo()));
                    //更新首页常用tab
                    EventBus.getDefault().post(new UpdateFavouriteEvent(isFavourite ? UpdateFavouriteEvent.TYPE_ADD : UpdateFavouriteEvent.TYPE_CANCEL));
                }
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);
                btnSetCommonSwitch.setChecked(mDevice.getIsFavourite());
            }
        };
        if(mDevice.getDeviceGroup() == 0){
            data.put("deviceSerialNoList", selectedSerialNos);
            LLHttpManager.deviceIsFavourite(mContext, data,callback);
        }else{
            data.put("deviceGroupSerialNos", selectedSerialNos);
            LLHttpManager.deviceGroupIsFavourite(mContext, data,callback);
        }
    }
}
